home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / sprtxtrn.sea / Support Tools eXternals 1.2.5 / card_31988.txt < prev    next >
Text File  |  1990-11-13  |  9KB  |  279 lines

  1. -- card: 31988 from stack: in.5
  2. -- bmap block id: 35406
  3. -- flags: 0000
  4. -- background id: 3858
  5. -- name: DiskSpaceAvail
  6. ----- HyperTalk script -----
  7. on HideObjects
  8.   hide cd btn "Try It!"
  9. end HideObjects
  10.  
  11. on ShowObjects
  12.   show cd btn "Try It!"
  13. end ShowObjects
  14.  
  15.  
  16. -- part 6 (button)
  17. -- low flags: 00
  18. -- high flags: A002
  19. -- rect: left=78 top=197 right=231 bottom=171
  20. -- title width / last selected line: 0
  21. -- icon id / first selected line: 0 / 0
  22. -- text alignment: 1
  23. -- font id: 0
  24. -- text size: 12
  25. -- style flags: 8192
  26. -- line height: 16
  27. -- part name: Try it!
  28. ----- HyperTalk script -----
  29. on mouseUp
  30.   put volumePath() into newVolume
  31.   if newVolume = empty then exit mouseUp
  32.   put DiskSpaceAvail(newVolume) into freeSpace
  33.   answer freeSpace && "bytes (" & freeSpace div 1024 && "K) free on ΓÇ£" & newVolume & "ΓÇ¥"
  34. end mouseUp
  35.  
  36.  
  37.  
  38.  
  39. -- part contents for background part 20
  40. ----- text -----
  41. This XFCN returns the amount of free space (in bytes) on a disk.  It is functionally similar to HyperCard's "DiskSpace", except that it works with any mounted volume
  42.  
  43. To use this demo click on the "Try It!" button.
  44. You will be presented with a dialog to select one of your mounted drives.
  45.  
  46. Calling syntax : DiskSpaceAvail(volumeName, <"noDialog:"errorGlobal>)
  47.   VOLUMENAME: the path to the volume to check.
  48.  
  49.  
  50. -- part contents for background part 38
  51. ----- text -----
  52. 10/50
  53.  
  54. -- part contents for background part 42
  55. ----- text -----
  56. { DiskSpaceAvail(volumeName ┬½,ΓÇ£noDialog:ΓÇ¥errorGlobal┬╗)  }
  57. { XFCN to determin amount of free space on a volume, returns  }
  58. { the number of bytes free                          }
  59. {}
  60. {  brought to you by:  Anup Murarka      Eric Carlson    }
  61. {            ALINK:  SKEPTIC      ALINK:  cyNic  }
  62. {                  CIS:  76004,3356    }
  63. {}
  64. {        We are part of the Support Tools Development Group,  }
  65. {        Apple Computer, Inc.  }
  66. {}
  67. {        please DO NOT contack Mac DTS for support of this code!  }
  68. {}
  69. {        please DO contact the authors for support of this code!  }
  70. {}
  71. {        Send comments, bug reports, requests to any of the above  }
  72. {        E-mail addresses or to:}
  73. {}
  74. {              (one of us)          }
  75. {              Apple Computer, Inc.    }
  76. {              900 E. Hamilton, Ave.    }
  77. {              Campbell, CA   95008    }
  78. {              M/S 72-L          }
  79. {}
  80. {  Copyright:  ┬⌐ 1989, 1990 by Apple Computer, Inc., all rights reserved.  }
  81. {}
  82. { written by Eric Carlson                    }
  83. { AppleLink:  cyNic                        }
  84. { modification history                       }
  85. {       Date        Initials                  Comments                  }
  86. {       ----        ------    ------------------------------------------------------  }
  87. {    1/26/90        ec      first written                                  }
  88. {    2/14/90      ec      general code cleaning                            }
  89. {    5/31/90      ec      commented code further                          }
  90. {    7/2/90      ec      fixed unsinged/signed conversion bug as per Tech note 157, changed version }
  91. {                                  to 1.1      }
  92. {}
  93.  
  94. unit DiskSpaceAvail;
  95.  
  96. interface
  97.  
  98.   uses
  99.     HyperXcmd;
  100.  
  101.   procedure MAIN (paramPtr: XCmdPtr);
  102.  
  103. implementation
  104.  
  105.   procedure DiskSpaceAvail (paramPtr: XCmdPtr);
  106.   FORWARD;
  107.  
  108.   procedure MAIN (paramPtr: XCmdPtr);
  109.   begin
  110.     DiskSpaceAvail(paramPtr);
  111.   end;
  112.  
  113.   procedure reportToUser (paramPtr: XCmdPtr;
  114.                   msgStr: str255);
  115. {}
  116. { report something back to the user.  }
  117. { the last parameter (optional) to an external may contain }
  118.  { "noDialog" or "noDialog:GlobalName".  GlobalName is the name }
  119.  { of a HyperTalk global variable into which error messages will be }
  120.  { placed.  we've decided to use this approach to avoid confusing }
  121. { an error message with a valid result being returned from an XFCN. }
  122. {}
  123.     var
  124.       tempStr: str255;
  125.   begin
  126. {check the last param to see if the user requested that}
  127. { we suppress the error dialog }
  128.     ZeroToPas(paramPtr, paramPtr^.params[paramPtr^.paramCount]^, tempStr);
  129.     UprString(tempStr, true);
  130.     if pos('NODIALOG', tempStr) = 0 then
  131.   { no special error handling specified, throw up a dialog and return the error message }
  132.       begin
  133.         SendCardMessage(paramPtr, concat('answer "', msgStr, '"'));
  134.         paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
  135.       end
  136.     else if (pos(':', tempStr) > 0) then
  137.   { requested global AND noDialog so we fill in the global and return empty }
  138.       begin
  139.         tempStr := copy(tempStr, pos(':', tempStr) + 1, length(tempStr));
  140.                             { get the name of the HC global  to fill }
  141.         SetGlobal(paramPtr, tempStr, PasToZero(paramPtr, msgStr));
  142.                             { and fill it }
  143.         paramPtr^.returnValue := PasToZero(paramPtr, '');  { return empty }
  144.       end
  145.     else
  146.   { requested noDialog only so we return the error condition as the result }
  147.       paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
  148.   end;  { procedure }
  149.  
  150.   function NumberToString (paramPtr: XCmdPtr;
  151.                   num: LONGINT): Str255;
  152. { use the toolbox call rather than HC's }
  153.     var
  154.       tempStr: str255;
  155.   begin
  156.     NumToString(num, tempStr);
  157.     NumberToString := tempStr;
  158.   end;
  159.  
  160.   procedure reportResError (paramPtr: XCmdPtr;
  161.                   errorNum: integer);
  162.     var
  163.       errMsg, tempName: str255;
  164.   begin
  165.     sysbeep(40);
  166.     case errorNum of          { what caused the problem? }
  167.       bdNamErr: 
  168.         errMsg := 'Bad volume name.';
  169.       extFSErr: 
  170.         errMsg := 'External file system.';
  171.       ioErr: 
  172.         errMsg := 'I/O Error.';
  173.       nsDrvErr: 
  174.         errMsg := 'No such drive.';
  175.       nsvErr: 
  176.         errMsg := 'No such volume.';
  177.       paramErr: 
  178.         errMsg := 'You may not eject the default volume.';
  179.       otherwise
  180.         errMsg := concat('unexpected error #', NumberToString(paramPtr, errorNum));
  181.     end;    { case }
  182.  
  183.     errMsg := concat('Sorry, ', errMsg);
  184.     reportToUser(paramPtr, errMsg);
  185.     { return the error message }
  186.   end;    { function }
  187.  
  188.   function getParams (paramPtr: XCmdPtr;
  189.                   var volToFind: str255): boolean;
  190.     var
  191.       numParams: integer;
  192.       inputCh: str255;
  193.   begin
  194.     getParams := true;
  195.     numParams := paramPtr^.paramCount;
  196.     if (numParams < 1) or (numParams > 2) then
  197.       begin
  198.         getParams := false;
  199.         reportToUser(paramPtr, 'DiskSpaceAvail(volumeName ┬½,ΓÇ£noDialog:ΓÇ¥errorGlobal┬╗)');
  200.         exit(getParams);
  201.       end;
  202.     MoveHHi(paramPtr^.Params[1]);
  203.     HLock(paramPtr^.Params[1]);
  204.     ZeroToPas(paramPtr, paramPtr^.Params[1]^, volToFind);
  205.     HUnlock(paramPtr^.Params[1]);
  206.     uprString(volToFind, true);
  207.     if (volToFind = '?') or (volToFind = '') then
  208.       begin
  209.         getParams := false;
  210.         reportToUser(paramPtr, 'DiskSpaceAvail(volumeName ┬½,ΓÇ£noDialog:ΓÇ¥errorGlobal┬╗)');
  211.         exit(getParams);
  212.       end;
  213.     if (volToFind = '!') then
  214.       begin
  215.         getParams := false;
  216.         reportToUser(paramPtr, 'v1.1, ┬⌐ 1990 Apple Computer, Inc., by Eric Carlson');
  217.         exit(getParams);
  218.       end;
  219.   end;    {GetParams}
  220.  
  221.   function validVolumeName (volumeName: str255): str255;
  222.   { a volume name must have one (and only one) colon in it, as }
  223.   { the last character }
  224.   begin
  225.     if pos(':', volumeName) = 0 then
  226.       validVolumeName := concat(volumeName, ':')
  227.     else
  228.       validVolumeName := copy(volumeName, 1, pos(':', volumeName));
  229.   end;
  230.  
  231.   procedure DiskSpaceAvail (paramPtr: XCmdPtr);
  232.     type
  233.       TwoIntsMakeaLong = record
  234.           case integer of
  235.             1: (
  236.                 long: LongInt
  237.             );
  238.             2: (
  239.                 ints: array[0..1] of integer
  240.             );
  241.         end;
  242.     var
  243.       theSpace: TwoIntsMakeaLong;
  244.       getParamsOK: boolean;
  245.       volToTest: str255;
  246.       PB: HParamBlockRec;
  247.       errorCode: OSErr;
  248.  
  249.   begin
  250.   { fetch and validate the passed parameters}
  251.     getParamsOK := getParams(paramPtr, volToTest);
  252.     if not (getParamsOK) then
  253.       exit(DiskSpaceAvail);
  254.  
  255.     volToTest := validVolumeName(volToTest);      { make sure the volume name is correct }
  256.  
  257.   { initialize parameter block.  Since volToTest is a full pathname, no other field is needed}
  258.     zeroBytes(paramPtr, @PB, sizeOf(PB));
  259.     PB.ioNamePtr := @volToTest;
  260.     PB.ioVolIndex := -1;
  261.     errorCode := PBHGetVInfo(@PB, false);
  262.  
  263.     if errorCode = noErr then
  264.       begin
  265. { we must change the unsinged int returned by our PBH call to a signed long ourselves as the compiler }
  266. {  will sign extend it and thus make it negative - see tech note 157 }
  267.         theSpace.ints[0] := 0;                { clear the high word }
  268.         theSpace.ints[1] := PB.ioVFrBlk;          { set the low word to our value }
  269.         NumToString(theSpace.long * PB.ioVAlBlkSiz, volToTest);  { borrow our string var for a minute }
  270.         paramPtr^.returnValue := PasToZero(paramPtr, volToTest);
  271.       end
  272.     else
  273.       begin
  274.         reportResError(paramPtr, errorCode);
  275.         exit(DiskSpaceAvail);
  276.       end;
  277.   end;    { procedure FileExists}
  278.  
  279. end.  { unit FileExists}